const Mock = require('mockjs')

const data = Mock.mock({
  'items|20': [{
    'id|+1': 0,
    name: '@cword(2,4)',
    url: '@string("lower",4,6)',
    api: '@string("lower",4,6)',
    type: '@integer(0,1)',
    code: '@string("number",4)',
    'method|1': ['get', 'post'],
    'openIcon|1': ['404', 'bug', 'chart', 'clipboard', 'component', 'dashboard', 'documentation', 'donate', 'drag', 'edit', 'education', 'email', 'example', 'excel', 'exit-fullscreen', 'eye-open', 'tree-table', 'eye', 'form', 'fullscreen', 'guide', 'icon', 'international', 'language', 'link', 'list', 'lock', 'message', 'money', 'nested', 'password', 'pdf', 'people', 'peoples', 'qq', 'search', 'shopping', 'size', 'skill', 'star', 'tab', 'table', 'theme', 'tree', 'user', 'wechat', 'zip',],
    'closeIcon|1': ['404', 'bug', 'chart', 'clipboard', 'component', 'dashboard', 'documentation', 'donate', 'drag', 'edit', 'education', 'email', 'example', 'excel', 'exit-fullscreen', 'eye-open', 'tree-table', 'eye', 'form', 'fullscreen', 'guide', 'icon', 'international', 'language', 'link', 'list', 'lock', 'message', 'money', 'nested', 'password', 'pdf', 'people', 'peoples', 'qq', 'search', 'shopping', 'size', 'skill', 'star', 'tab', 'table', 'theme', 'tree', 'user', 'wechat', 'zip',],
    'parentId|1': [null, '@integer(0,9)'],
    state: '@integer(0,1)',
  }]
})

module.exports = [
  //分页查询资源
  {
    url: '/api/reso/list',
    type: 'get',
    response: config => {
      const items = data.items

      const { name, pageSize, pageNum } = config.query
      const mockList = items.filter(resource => {
        if (!name || resource.name == name) return true
        return false
      })
      const pageList = mockList.filter((item, index) => index < pageSize * pageNum && index >= pageSize * (pageNum - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  //根据id删除资源
  {
    url: '/api/reso/deleteById',
    type: 'get',
    response: config => {
      const { id } = config.query
      data.items = data.items.filter(resource => resource.id != id)
      return {
        code: 20000,
        message: "删除成功"
      }
    }
  },
  //新增资源
  {
    url: '/api/reso/addReso',
    type: 'post',
    response: config => {
      const { resource } = config.body
      resource.id = data.items[data.items.length - 1].id + 1
      data.items.push(resource)
      return {
        code: 20000,
        message: "添加成功"
      }
    }
  },
  //更新资源
  {
    url: '/api/reso/updateReso',
    type: 'post',
    response: config => {
      const { resource } = config.body
      data.items.forEach((item, index) => {
        if (item.id == resource.id) {
          data.items.splice(index, 1, resource)
        }
      })
      return {
        code: 20000,
        message: "修改成功"
      }
    }
  },
  //根据url查找资源
  {
    url: '/api/reso/queryResoByExample',
    type: 'post',
    response: config => {
      const { resource } = config.body
      let find = null
      data.items.forEach(item => {
        if (
          (!resource.api || item.api == resource.api)
          && (!resource.code || item.code == resource.code)
          && (!resource.id || item.id == resource.id)
          && (!resource.url || item.url == resource.url)
        ) {
          find = { ...item }
          return
        }
      })
      return {
        code: 20000,
        data: {
          resource: find
        }
      }
    }
  },
]
